<?php
namespace jy_merch;
defined('BY_JYA') or exit('error');
/*
	钱包提现
*/
class creditcash extends \plugin{
	/*
		创建结算单
	*/
	public function create_balance($mid,$postdata=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if(!$postdata['money']){
			exi('请输入提现金额','error');
		}
		if($postdata['money']<0){
			exi('提现金额必须大于0','error');
		}
		$merch=pdo_get('jy_merch_user',array('id'=>$postdata['merchid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$postdata['isjifen'] && $merch['credit2']<$postdata['money']){
			exi('商户钱包余额不足！','error');
		}
		if($postdata['isjifen'] && $merch['credit1']<$postdata['money']){
			exi('商户钱包积分不足！','error');
		}
		if($postdata['pay_type']=='downline'){
				if(!$postdata['wechat']){
					exi('请先填写微信号！','error');
				}
				if(!$postdata['mobile']){
					exi('请先填写联系电话！','error');
				}
		}
		if($postdata['pay_type']=='alipay'){
				if(!$postdata['realname']){
					exi('请先填写真实姓名！','error');
				}
				if(!$postdata['alipay']){
					exi('请先填写支付宝帐号！','error');
				}
				if(!$postdata['alipay1']){
					exi('请确认帐号','error');
				}
				if($postdata['alipay1']!=$postdata['alipay']){
					exi('确认帐号与支付宝帐号不匹配！','error');
				}
				$typetitle='支付宝';
		}
		if($postdata['pay_type']=='union'){
				if(!$postdata['realname']){
					exi('请输入姓名','error');
				}
				if(!$postdata['bankname']){
					exi('请输入所属银行','error');
				}
				if(!$postdata['bankcard']){
					exi('请银行卡号','error');
				}
				if(!$postdata['bankcard1']){
					exi('请确认卡号','error');
				}
				if($postdata['bankcard']!=$postdata['bankcard1']){
					exi('确认卡号与银行卡号不匹配！','error');
				}
				$typetitle='银行卡';
		}
		if($postdata['pay_type']=='wechat' && $postdata['finalprice']>2000){
			if(!$postdata['realname']){
				exi('请输入真实姓名，微信打款（满2000后必须提供真实姓名）','error');
			}
		}
		$new_balance=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$merch['mid'],
				'balancesn'=>get_ordersn($this->tablename('creditcash'),'balancesn','AC'),
				'money'=>$postdata['money'],
				'pay_type'=>$postdata['pay_type'],
				'realname'=>$postdata['realname'],
				'alipay'=>$postdata['alipay'],
				'bankname'=>$postdata['bankname'],
				'bankcard'=>$postdata['bankcard'],
				'wechat'=>$postdata['wechat'],
				'mobile'=>$postdata['mobile'],
				'isjifen'=>$postdata['isjifen'],
				'createtime'=>TIMESTAMP,
				'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$new_balance['merchid']=$postdata['merchid'];
		$this->insert('creditcash',$new_balance);
		$balance_id=pdo_insertid();
		if($postdata['isjifen']){
			pdo_update('jy_merch_user',array('credit1 -='=>$new_balance['final_money']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$new_balance['merchid']));
		}else{
			pdo_update('jy_merch_user',array('credit2 -='=>$new_balance['final_money']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$new_balance['merchid']));
		}
		$feelog=array(
				'mid'=>$new_balance['mid'],
				'merchid'=>$new_balance['merchid'],
				'msg'=>$postdata['isjifen']?'商户钱包积分提现消耗':'商户钱包余额提现消耗',
				'type'=>$postdata['isjifen']?1:3,
				'gift_type'=>0,
				'fee'=>$new_balance['final_money'],
				'status'=>0,
		);
		$type=$postdata['isjifen']?5:3;
		$this->model('model')->set_feelog($feelog,$type);
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('creditcash',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type){
		$balance=$this->get('creditcash',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn']);
		$update=array(
			'status'=>3,
			'pay_type'=>$pay_type,
			'pay_trade_no'=>$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		$this->update('creditcash',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn='',$realname=''){
		$params=array(
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'remark'=>'代理商佣金发放',
			'realname'=>$realname,
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
}